{% extends 'base.html' %}

{% block title %}编辑碳排放因子{% endblock %}

{% block content %}
<div class="d-flex justify-content-between align-items-center mb-4">
    <h1>编辑碳排放因子</h1>
    <div>
        <a href="{{ url_for('factor.factor_list') }}" class="btn btn-outline-secondary">返回列表</a>
    </div>
</div>

<div class="card mb-4">
    <div class="card-header">
        <ul class="nav nav-tabs card-header-tabs" id="factorTabs" role="tablist">
            <li class="nav-item" role="presentation">
                <button class="nav-link active" id="info-tab" data-bs-toggle="tab" data-bs-target="#info" type="button" role="tab" aria-controls="info" aria-selected="true">基本信息</button>
            </li>
            <li class="nav-item" role="presentation">
                <button class="nav-link" id="coefficients-tab" data-bs-toggle="tab" data-bs-target="#coefficients" type="button" role="tab" aria-controls="coefficients" aria-selected="false">关联系数</button>
            </li>
            <li class="nav-item" role="presentation">
                <button class="nav-link" id="documents-tab" data-bs-toggle="tab" data-bs-target="#documents" type="button" role="tab" aria-controls="documents" aria-selected="false">证明材料</button>
            </li>
        </ul>
    </div>
    <div class="card-body">
        <div class="tab-content" id="factorTabsContent">
            <!-- 基本信息标签页 -->
            <div class="tab-pane fade show active" id="info" role="tabpanel" aria-labelledby="info-tab">
                <form method="post" action="{{ url_for('factor.edit_factor', id=factor.id) }}">
                    <div class="row mb-3">
                        <div class="col-md-6">
                            <label for="name" class="form-label">因子名称 <span class="text-danger">*</span></label>
                            <input type="text" class="form-control" id="name" name="name" value="{{ factor.name }}" required>
                        </div>
                        <div class="col-md-6">
                            <label for="gas_type" class="form-label">气体种类 <span class="text-danger">*</span></label>
                            <input type="text" class="form-control" id="gas_type" name="gas_type" value="{{ factor.gas_type }}" required>
                        </div>
                    </div>

                    <div class="row mb-3">
                        <div class="col-md-6">
                            <label for="value_type" class="form-label">值类型 <span class="text-danger">*</span></label>
                            <select class="form-select" id="value_type" name="value_type" required>
                                <option value="直接排放" {% if factor.value_type == '直接排放' %}selected{% endif %}>直接排放</option>
                                <option value="间接排放" {% if factor.value_type == '间接排放' %}selected{% endif %}>间接排放</option>
                            </select>
                        </div>
                        <div class="col-md-6">
                            <label for="emission_value" class="form-label">碳排值 <span class="text-danger">*</span></label>
                            <input type="number" step="any" class="form-control" id="emission_value" name="emission_value" value="{{ factor.emission_value }}" required>
                        </div>
                    </div>

                    <div class="row mb-3">
                        <div class="col-md-6">
                            <label for="factor_type" class="form-label">因子类型 <span class="text-danger">*</span></label>
                            <select class="form-select" id="factor_type" name="factor_type" required>
                                <option value="标准因子" {% if factor.factor_type == '标准因子' %}selected{% endif %}>标准因子</option>
                                <option value="自建因子" {% if factor.factor_type == '自建因子' %}selected{% endif %}>自建因子</option>
                            </select>
                        </div>
                        <div class="col-md-6">
                            <label for="category_id" class="form-label">分类 <span class="text-danger">*</span></label>
                            <select class="form-select" id="category_id" name="category_id" required>
                                <option value="">-- 选择分类 --</option>
                                {% for category in categories %}
                                <option value="{{ category.id }}" {% if factor.category_id == category.id %}selected{% endif %}>
                                    {{ '└─ ' * (category.level - 1) }}{{ category.name }}
                                </option>
                                {% endfor %}
                            </select>
                        </div>
                    </div>

                    <div class="mb-3">
                        <label for="source" class="form-label">来源</label>
                        <input type="text" class="form-control" id="source" name="source" value="{{ factor.source }}">
                    </div>

                    <div class="mb-3">
                        <label for="formula_description" class="form-label">碳排因子公式说明</label>
                        <textarea class="form-control" id="formula_description" name="formula_description" rows="3">{{ formula.formula_description if formula else '' }}</textarea>
                    </div>

                    <div class="d-grid gap-2 d-md-flex justify-content-md-end">
                        <button type="submit" class="btn btn-primary">保存</button>
                    </div>
                </form>
            </div>

            <!-- 关联系数标签页 -->
            <div class="tab-pane fade" id="coefficients" role="tabpanel" aria-labelledby="coefficients-tab">
                <div class="card mb-4">
                    <div class="card-body">
                        <h5 class="card-title">添加关联系数</h5>
                        <div class="input-group">
                            <input type="text" id="coefficientSearch" class="form-control" placeholder="搜索碳排放系数...">
                            <button class="btn btn-outline-secondary" type="button" id="searchCoefficientBtn">搜索</button>
                        </div>
                        <div id="coefficientSearchResults" class="mt-3"></div>
                    </div>
                </div>

                <div class="card">
                    <div class="card-body">
                        <h5 class="card-title">已关联系数</h5>
                        {% if associated_coefficients %}
                        <div class="table-responsive">
                            <table class="table table-striped">
                                <thead>
                                    <tr>
                                        <th>系数名称</th>
                                        <th>气体</th>
                                        <th>值</th>
                                        <th>单位</th>
                                        <th>来源</th>
                                        <th>操作</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    {% for coeff in associated_coefficients %}
                                    <tr>
                                        <td>{{ coeff.name }}</td>
                                        <td>{{ coeff.gas_type }}</td>
                                        <td>{{ coeff.value }}</td>
                                        <td>{{ coeff.unit }}</td>
                                        <td>{{ coeff.source }}</td>
                                        <td>
                                            <form method="post" action="{{ url_for('factor.remove_coefficient', factor_id=factor.id, coefficient_id=coeff.id) }}" class="d-inline">
                                                <button type="submit" class="btn btn-sm btn-outline-danger" onclick="return confirm('确定要移除该系数关联吗？');">移除</button>
                                            </form>
                                        </td>
                                    </tr>
                                    {% endfor %}
                                </tbody>
                            </table>
                        </div>
                        {% else %}
                        <p class="text-muted">暂无关联的碳排放系数</p>
                        {% endif %}
                    </div>
                </div>
            </div>

            <!-- 证明材料标签页 -->
            <div class="tab-pane fade" id="documents" role="tabpanel" aria-labelledby="documents-tab">
                <div class="card mb-4">
                    <div class="card-body">
                        <h5 class="card-title">上传证明材料</h5>
                        <form method="post" action="{{ url_for('factor.upload_document', factor_id=factor.id) }}" enctype="multipart/form-data">
                            <div class="mb-3">
                                <input class="form-control" type="file" id="document" name="document" accept=".pdf,.doc,.docx,.xls,.xlsx,.jpg,.jpeg,.png">
                                <div class="form-text">支持的文件类型：pdf, doc, docx, xls, xlsx, jpg, jpeg, png</div>
                            </div>
                            <button type="submit" class="btn btn-primary">上传</button>
                        </form>
                    </div>
                </div>

                <div class="card">
                    <div class="card-body">
                        <h5 class="card-title">已上传证明材料</h5>
                        {% if documents %}
                        <div class="list-group">
                            {% for doc in documents %}
                            <div class="list-group-item d-flex justify-content-between align-items-center">
                                <div>
                                    <a href="{{ url_for('factor.view_document', filename=doc.file_path) }}" target="_blank">{{ doc.file_name }}</a>
                                    <small class="text-muted d-block">{{ doc.uploaded_at }}</small>
                                </div>
                                <form method="post" action="{{ url_for('factor.remove_document', doc_id=doc.id) }}" class="d-inline">
                                    <button type="submit" class="btn btn-sm btn-outline-danger" onclick="return confirm('确定要删除该文件吗？');">删除</button>
                                </form>
                            </div>
                            {% endfor %}
                        </div>
                        {% else %}
                        <p class="text-muted">暂无证明材料</p>
                        {% endif %}
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<script>
// 系数搜索功能
document.getElementById('searchCoefficientBtn').addEventListener('click', searchCoefficients);
document.getElementById('coefficientSearch').addEventListener('keypress', function(e) {
    if (e.key === 'Enter') {
        searchCoefficients();
    }
});

function searchCoefficients() {
    const query = document.getElementById('coefficientSearch').value.trim();
    // 直接在Jinja模板中生成完整URL，避免字符串替换操作
    fetch(`{{ url_for('factor.search_coefficients', factor_id=factor.id) }}?query=${encodeURIComponent(query)}`)
        .then(response => response.text())
        .then(html => {
            document.getElementById('coefficientSearchResults').innerHTML = html;
        });
}
</script>
{% endblock %}